<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="用户名" prop="name">
        <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="IP地址" prop="ipAdress">
        <el-input v-model="dataForm.ipAdress" placeholder="" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="绑定范围" prop="description">
        <el-input v-model="dataForm.startIp" placeholder="" style="width:49%"></el-input>
        ~
        <el-input v-model="dataForm.endIp" placeholder="" style="width:48%"></el-input>
      </el-form-item>
      <el-form-item label="排除IP" prop="description">
        <el-input type="textarea" v-model="dataForm.ipnet" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="MAC地址" prop="macAdress">
        <el-input type="textarea" v-model="dataForm.macAdress" placeholder=""></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      var validateIpAddress = (rule, value, callback) => {
        var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
        if (!reg.test(value)) {
          callback(new Error('IP地址格式不正确，请检查'))
        } else {
          callback()
        }
      }
      var validateMacAddress = (rule, value, callback) => {
        const str = /^[A-F0-9]{2}(-[A-F0-9]{2}){5}$|^[A-F0-9]{2}(:[A-F0-9]{2}){5}$/
        // const regexp = new RegExp(str)
        if (!str.test(value)) {
          callback(new Error('Mac地址格式不正确，请检查'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        disabled: false,
        menuList: [],
        orgList: this.treeDataTranslate(JSON.parse(sessionStorage.getItem('orgList') || '[]'), 'orgNo', 'parentNo'),
        menuListTreeProps: {
          label: 'name',
          children: 'children'
        },
        orgListTreeProps: {
          label: 'orgName',
          children: 'children'
        },
        dataForm: {
          id: 0,
          name: '',
          ipAdress: '',
          macAdress: '',
          isUnique: '1',
          description: '',
          userId: '',
          oldIp: '',
          vlan: ''
        },
        dataRule: {
          name: [
            {required: true, message: '名称不能为空', trigger: 'blur'}
          ],
          ipAdress: [
            {validator: validateIpAddress, trigger: 'change'}
          ],
          macAdress: [
            {validator: validateMacAddress, trigger: 'change'}
          ]
        }
      }
    },
    computed: {
      userId: {
        get () {
          return this.$store.state.user.id
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        // this.disabled = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        if (this.dataForm.id) {
          // this.disabled = true
          this.$http({
            url: `/ipmacbind/ip-mac-bind/info/${this.dataForm.id}`,
            method: 'get'
          }).then(({data}) => {
            if (data && data.code === 0) {
              data.log.isUnique = data.log.isUnique.toString()
              this.dataForm = data.log
              this.dataForm.oldIp = data.log.ipAdress
            }
          })
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.dataForm.isUnique = parseInt(this.dataForm.isUnique)
        this.dataForm.userId = this.userId
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: `/ipmacbind/ip-mac-bind/${!this.dataForm.id ? 'save' : 'update'}`,
              method: 'post',
              data: this.dataForm
            }).then(({data}) => {
              this.$message({
                message: data.msg,
                type: data.code === 0 ? 'success' : 'warning',
                duration: 1500
              })
              this.visible = false
              this.$emit('refreshDataList')
              // if (data && data.code === 0) {
              //   if (data.msg === '操作成功') {
              //     this.$message({
              //       message: data.msg,
              //       type: 'success',
              //       duration: 1500
              //     })
              //     this.visible = false
              //     this.$emit('refreshDataList')
              //   } else {
              //     this.$message({
              //       message: data.msg,
              //       type: 'warning',
              //       duration: 1500
              //     })
              //   }
              // }
            })
          }
        })
      }
    }
  }
</script>
